package com.xxx.utli;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadOss {
//    oss域名
    public static final String ALI_OOMAIN = "https://wechatmall1.oss-cn-guangzhou.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {

//        生成文件名
        String originalFilename = file.getOriginalFilename(); //原图片名
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + ext;

//        地域节点
        String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tJUT2rN6iR4ytPGFY7q";
        String accessKeySecret = "VfB0BuaZBSZor0raYpEUvb8Ijcz1vY";

//        OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(
                "wechatmall1", //仓库名
                fileName, //文件名
                file.getInputStream()
        );

        ossClient.shutdown();
        return ALI_OOMAIN + fileName;
    }

}
